<?php

class CLI {
	const BLACK = 30;
	const RED = 31;
	const GREEN = 32;
	const YELLOW = 33;
	const BLUE = 34;
	const MAGENTA = 35;
	const CYAN = 36;
	const WHITE = 37;
	
	const UP = "A";
	const DOWN = "B";
	const RIGHT = "C";
	const LEFT = "D";
	
	const A_LEFT = "G";
		
	public static function colorEcho($what, $newLine = true, $fgColor=self::WHITE, $bgColor=self::BLACK, $bold=false) {
		$bgColor+=10;
		
		$bold=$bold?"1;":false;		
		echo "\033[{$bold}{$bgColor};{$fgColor}m{$what}\033[0m";
		
		if($newLine)
		echo "\r\n";
	}
	
	public static function move($N, $direction) {
		echo "\033[{$N}{$direction}";
	}
	
	public static function moveTo($x=0, $y=0) {
		echo "\033[{$y};{$x}H";
	}
	
	public static function clear() {
		echo "\033[2J";
	}
	
	public static function clearToEOL() {
		echo "\033[K";
	}
	
	public static function saveCursorPosition() {
		echo "\033[s";
	}
	
	public static function restoreCursorPosition() {
		echo "\033[u";
	}
	
	public static function NL($num=1) {
		for($i=0; $i<$num; $i++)
		echo "\r\n";
	}
	
	public static function showColors() {
		$colors = array(
			self::BLACK,	
			self::RED,	
			self::GREEN,	
			self::YELLOW,	
			self::BLUE,	
			self::MAGENTA,	
			self::CYAN,	
			self::WHITE	
		);
		
		foreach($colors as $color) {
			self::colorEcho("Test", true, $color);
			self::colorEcho("Test", true, $color, self::BLACK, true);
		}
	}
	
	public static function startMSG($what) {
		self::colorEcho($what, false, self::WHITE, self::BLACK, true);
	}
	
	public static function finishMSG($what, $color=self::GREEN) {
		self::move(60, self::A_LEFT);
		self::colorEcho("[  ", false);
		self::colorEcho($what, false, $color, self::BLACK, true);
		self::colorEcho("  ]", true);
	}
}